cmake_minimum_required(VERSION 3.17)
project(qt_test)

set(CMAKE_CXX_STANDARD 14)
# 设置Qt前缀路径
set(CMAKE_PREFIX_PATH "D:/app/Qt/6.1.1/mingw81_64")
# 自动处理Qt中使用了Q_OBJECT宏的头文件
set(CMAKE_AUTOMOC ON)
# 自动处理Qt的rc资源文件
set(CMAKE_AUTORCC ON)
# 自动处理Qt的ui文件
set(CMAKE_AUTOUIC ON)

# 设置Qt版本
set(QT_VERSION 6)
set(QT_MINOR_VERSION 0)
# 设置项目需要用到的Qt模块
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt6::Core Qt6::Gui Qt6::Widgets)

# 添加WIN32平台标识，可以关闭在双击运行应用程序时产生的控制台窗口
add_executable(${PROJECT_NAME} WIN32
        main.cpp
        logindialog.cpp
        logindialog.h
        logindialog.ui
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        )

if(NOT CMAKE_PREFIX_PATH)
    message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
                    "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif()
# 查找Qt的包
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
# 将Qt的库链接到可执行文件
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})

# Windows平台相关的设置，如果是Debug则拷贝的库是带有d标志的dll
if(WIN32)
    set(DEBUG_SUFFIX)
    # 为了解决Qt5.14 MinGW不再提供带d的dll而新增加两个条件限制一下，QT_MINOR_VERSION是在前面设定Qt版本时自定义的
    if (CMAKE_BUILD_TYPE MATCHES "Debug" AND QT_VERSION LESS_EQUAL 5 AND QT_MINOR_VERSION LESS 14)
        set(DEBUG_SUFFIX "d")
    endif()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif()
    endif()
    # 拷贝windows相关的库到目标文件所在的目录
    if(EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif()
    # 拷贝每个需要的Qt库到目标文件所在的目录
    foreach(QT_LIB ${REQUIRED_LIBS})
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach(QT_LIB)
endif()
